package org.smi.answer;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;

public class StringToXml {
    public static void main(String[] args) {
        // 原始字符串
        String originalString = "<?xml version='1.0' encoding='UTF-8'?><TRANSACTION><MESSAGE_ESB_HEAD><SOURCE_SYSTEM_ID>CAP</SOURCE_SYSTEM_ID><ORIGINAL_SYSTEM_ID>CAP</ORIGINAL_SYSTEM_ID><TARGET_SYSTEM_ID>OCR</TARGET_SYSTEM_ID><SERVICE_ID>PAN0009</SERVICE_ID><SERVICE_VERSION>1.0.0</SERVICE_VERSION><SCENES_ID>001</SCENES_ID><SCENES_VERSION>1.0</SCENES_VERSION><TRAN_CODE>OCRIdCard</TRAN_CODE><COMM_TYPE>00</COMM_TYPE><TERMINAL_ID>192.168.99.72</TERMINAL_ID><CHANNEL_ID>90</CHANNEL_ID><GLOBAL_SEQ_NO>GCAP20240203</GLOBAL_SEQ_NO><SOURCE_SEQ_NO>CAP20240203</SOURCE_SEQ_NO><SOURCE_MACHINE_DATE>20240203</SOURCE_MACHINE_DATE><SOURCE_MACHINE_TIME>142710762</SOURCE_MACHINE_TIME><SOURCE_SECURITY_NODE></SOURCE_SECURITY_NODE><TARGET_SECURITY_NODE></TARGET_SECURITY_NODE><FILE_FLAG>0</FILE_FLAG><ESB_EXPAND_CONTENT></ESB_EXPAND_CONTENT><TARGET_RECIVE_DATE>20240205</TARGET_RECIVE_DATE><TARGET_RECIVE_TIME>082408653</TARGET_RECIVE_TIME><TARGET_MACHINE_DATE>20240205</TARGET_MACHINE_DATE><TARGET_MACHINE_TIME>082409624</TARGET_MACHINE_TIME><TARGET_SEQ_NO>0a0cd69781534353</TARGET_SEQ_NO><RET_COMM_STATUS>S</RET_COMM_STATUS><RET_SYSTEM_ID>OCR</RET_SYSTEM_ID><RET_CODE>000000</RET_CODE><RET_MSG>SUCCESS</RET_MSG></MESSAGE_ESB_HEAD><MESSAGE_APP_HEAD><BRANCH_CODE>20</BRANCH_CODE><CORPORATE_CODE></CORPORATE_CODE><OPERATOR_ID>20</OPERATOR_ID><CUSTOMER_ID></CUSTOMER_ID><SOURCE_BUSINESS_CODE></SOURCE_BUSINESS_CODE><ORIGINAL_BUSINESS_CODE></ORIGINAL_BUSINESS_CODE><SOURCE_TRAN_DATE>20240203</SOURCE_TRAN_DATE><SOURCE_TRAN_TIME>142710762</SOURCE_TRAN_TIME><AUTH_TELLER></AUTH_TELLER><AUTH_FALG></AUTH_FALG><AUTH_CODE></AUTH_CODE><AUTH_MSG></AUTH_MSG><PER_PAGE_NUM></PER_PAGE_NUM><CURR_PAGE_NUM></CURR_PAGE_NUM><PAGE_UPDOWN_FALG></PAGE_UPDOWN_FALG><APP_EXPAND_CONTENT></APP_EXPAND_CONTENT><TARGET_TRAN_DATE>20240205</TARGET_TRAN_DATE><TARGET_TRAN_TIME>082409624</TARGET_TRAN_TIME></MESSAGE_APP_HEAD><MESSAGE_BODY><RESPONSE><ISSUING_AUTHORITY>杞县公安局</ISSUING_AUTHORITY><ISSUE_DATE>20191007</ISSUE_DATE><EXPIRY_DATE>长期</EXPIRY_DATE></RESPONSE></MESSAGE_BODY></TRANSACTION>";

        // 格式化为XML
        String formattedXml = formatXml(originalString);
        System.out.println(formattedXml);
    }

    public static String formatXml(String xml) {
        try {
            // 创建文档构建器工厂
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // 创建文档构建器
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 解析字符串得到文档对象
            Document document = builder.parse(new InputSource(new StringReader(xml)));
            // 创建TransformerFactory
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            // 创建Transformer
            Transformer transformer = transformerFactory.newTransformer();
            // 设置换行缩进
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            // 创建StringWriter用于输出
            StringWriter writer = new StringWriter();
            // 创建DOMSource对象
            DOMSource domSource = new DOMSource(document);
            // 创建StreamResult对象
            StreamResult streamResult = new StreamResult(writer);
            // 将DOMSource对象转换为XML文件
            transformer.transform(domSource, streamResult);
            // 返回格式化后的XML字符串
            return writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
